//
//  UIs_Gradient_DemoVC.swift
//  APSwift
//
//  Created by jianbo.pei on 2022/2/11.
//

import UIKit

/// 渐变色
class UIs_Gradient_DemoVC: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        view.backgroundColor = .black
//        demo1()
//        demo2()
//        demo3()
        demo4()
    }
    
    private func demo1() {
        let gradientLayer = CAGradientLayer()
        gradientLayer.frame = CGRect(x: 10, y: 120, width: 200, height: 200)
        gradientLayer.backgroundColor = UIColor.gray.cgColor
        gradientLayer.type = .axial
        gradientLayer.startPoint = CGPoint(x: 0, y: 0)
        gradientLayer.endPoint = CGPoint(x: 0, y: 1)
        gradientLayer.colors = [UIColor.red.cgColor, UIColor.yellow.cgColor, UIColor.yellow.cgColor]
        gradientLayer.locations = [NSNumber.init(value: 0), NSNumber.init(value: 0.75), NSNumber.init(value: 1.0)]
        view.layer.addSublayer(gradientLayer)
    }
    
    private func demo2() {
        let gradientLayer = CAGradientLayer()
        gradientLayer.frame = CGRect(x: 10, y: 420, width: 200, height: 20)
//        gradientLayer.backgroundColor = UIColor.red.cgColor
        gradientLayer.type = .axial
        gradientLayer.startPoint = CGPoint(x: 0, y: 0)
        gradientLayer.endPoint = CGPoint(x: 1, y: 0)
//        gradientLayer.colors = [UIColor.red.cgColor, UIColor.yellow.cgColor, UIColor.yellow.cgColor]
        gradientLayer.colors = [UIColor.ap.ra(r: 0, a: 0).cgColor, UIColor.ap.ra(r: 0, a: 0.5).cgColor, UIColor.ap.ra(r: 0, a: 1.0).cgColor, UIColor.ap.ra(r: 0, a: 0.5).cgColor, UIColor.ap.ra(r: 0, a: 0).cgColor]
        gradientLayer.locations = [NSNumber.init(value: 0.01), NSNumber.init(value: 0.25), NSNumber.init(value: 5), NSNumber.init(value: 0.75), NSNumber.init(value: 1.0)]
        view.layer.addSublayer(gradientLayer)
    }
    
    private func demo3() {
        let gradientLayer = CAGradientLayer()
        gradientLayer.type = .axial
        gradientLayer.startPoint = CGPoint(x: 0.0, y: 0.5)
        gradientLayer.endPoint = CGPoint(x: 1.0, y: 0.5)
        gradientLayer.frame = CGRect(x: 50, y: 500, width: 70, height: 32)
        gradientLayer.colors = [UIColor.ap.rgb(r: 169, g: 255, b: 224), UIColor.ap.rgb(r: 254, g: 255, b: 193)]
        gradientLayer.cornerRadius = 16
        view.layer.addSublayer(gradientLayer)
    }
    
    private func demo4() {
        let lb = UILabel()
        lb.numberOfLines = 0
        let text = "由于Kingfisher里图片加载都是异步操作，我们希望可以监测到所有图片都加载完毕然后给一个回调，这其实就是多个异步任务的同步处理操作，而iOS里比较常用的也就是gcd里的信号量和group enter、group level了，关于这些知识网络上已经有比较多的资料了，这里就不再赘述了。废话不多说直接上代码。\n1、作者：liang1991\n2、链接：https://www.jianshu.com/p/623acaf06f97\n3、来源：简书\n4、著作权归作者所有。商业转载请联系作者获得授权，非商业转载请注明出处。"
        lb.font = UIFont.systemFont(ofSize: 20)
        let attStr = NSMutableAttributedString(string: text)
        lb.attributedText = attStr
        view.addSubview(lb)
        lb.snp.makeConstraints { make in
            make.left.equalTo(10)
            make.right.equalTo(-10)
            make.top.equalTo(160)
        }
        
        debugPrint("lb的行数 is \(lb.totalOfLines())")
    }

}

extension UILabel {
    /// - Returns: 总行数
      func totalOfLines() -> Int {
          
          if let attrText = self.attributedText {
              
              /// 创建CTFrame
              let setter: CTFramesetter = CTFramesetterCreateWithAttributedString(attrText)
              let path: CGMutablePath = CGMutablePath()
              path.addRect(CGRect(x: 0, y: 0, width: self.preferredMaxLayoutWidth, height: CGFloat.greatestFiniteMagnitude))
              let frame: CTFrame = CTFramesetterCreateFrame(setter, CFRangeMake(0, 0), path, nil)
              
              /// 获得CTLine数组
              let lines = CTFrameGetLines(frame)
              
              //获得行数
              let totalOfLines = CFArrayGetCount(lines)
              //Logger.log("totalOfLines: \(totalOfLines) :\(attrText.string)")
              return totalOfLines
          }
       
          return 0
      }
}
